<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java原子操作类Atomic深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
            transition: all 0.3s ease;
        }
        
        .feature-card:hover {
            transform: scale(1.02);
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .atomic-class-card {
            background: white;
            border: 2px solid transparent;
            background-clip: padding-box;
            position: relative;
            transition: all 0.3s ease;
        }
        
        .atomic-class-card::before {
            content: '';
            position: absolute;
            top: -2px;
            left: -2px;
            right: -2px;
            bottom: -2px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            z-index: -1;
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        
        .atomic-class-card:hover::before {
            opacity: 1;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 animate-pulse">
                    <i class="fas fa-atom mr-4"></i>Java原子操作类Atomic
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">深入理解并发编程中的线程安全利器</p>
                <div class="flex justify-center space-x-8 text-lg">
                    <div class="flex items-center">
                        <i class="fas fa-shield-alt mr-2"></i>
                        <span>线程安全</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-bolt mr-2"></i>
                        <span>高性能</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-lock-open mr-2"></i>
                        <span>无锁编程</span>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>多线程编程中，保证线程安全性是至关重要的。Java 提供了一系列的并发工具来帮助开发者处理多线程环境下的并发问题。当谈及原子操作类 Atomic 时，通常指的是 Java 并发编程中提供的一组原子类型，用于实现对共享变量的原子操作。这些原子类型包含了多种数据类型，如整型、长整型、布尔型以及引用类型等，可以有效地处理多线程环境下的并发问题，保证线程安全性。
            </p>
        </div>

        <!-- Atomic Overview Diagram -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">Atomic类体系结构</h2>
            <div class="mermaid">
                graph TD
                    A[Atomic原子类] --> B[基本类型]
                    A --> C[引用类型]
                    A --> D[数组类型]
                    A --> E[字段更新器]
                    
                    B --> B1[AtomicBoolean]
                    B --> B2[AtomicInteger]
                    B --> B3[AtomicLong]
                    
                    C --> C1[AtomicReference]
                    C --> C2[AtomicStampedReference]
                    C --> C3[AtomicMarkableReference]
                    
                    D --> D1[AtomicIntegerArray]
                    D --> D2[AtomicLongArray]
                    D --> D3[AtomicReferenceArray]
                    
                    E --> E1[AtomicIntegerFieldUpdater]
                    E --> E2[AtomicLongFieldUpdater]
                    E --> E3[AtomicReferenceFieldUpdater]
                    
                    style A fill:#667eea,stroke:#764ba2,stroke-width:3px,color:#fff
                    style B fill:#f093fb,stroke:#f5576c,stroke-width:2px
                    style C fill:#4facfe,stroke:#00f2fe,stroke-width:2px
                    style D fill:#fa709a,stroke:#fee140,stroke-width:2px
                    style E fill:#30cfd0,stroke:#330867,stroke-width:2px
            </div>
        </div>

        <!-- Significance of Atomic Operations -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-exclamation-circle mr-3 text-purple-600"></i>原子操作的意义
            </h2>
            <p class="text-lg leading-relaxed text-gray-700 mb-6">
                在多线程环境下，多个线程可能同时访问和修改共享变量，如果不加以控制，就会导致数据不一致、线程安全性问题。原子操作是一种不可中断的操作，要么全部执行成功，要么全部不执行，不会出现中间状态，从而保证了数据的一致性和线程安全性。
            </p>
            
            <!-- Example Card -->
            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-lg p-6 mb-6">
                <h3 class="text-xl font-semibold mb-4 text-purple-800">
                    <i class="fas fa-lightbulb mr-2"></i>举个简单的例子
                </h3>
                <p class="text-gray-700 mb-4">
                    假设有一个全局计数器 <code class="bg-purple-200 px-2 py-1 rounded">count</code>，多个线程同时对该计数器进行自增操作，并且未使用任何同步机制（如锁、原子操作等）。当多个线程同时执行自增操作时，可能会出现以下情况：
                </p>
                <ol class="list-decimal list-inside space-y-2 text-gray-700">
                    <li>线程 A 读取 <code class="bg-purple-200 px-2 py-1 rounded">count</code> 的值为 5，然后执行自增操作，将其加 1，得到 6。</li>
                    <li>同时，线程 B 也读取 <code class="bg-purple-200 px-2 py-1 rounded">count</code> 的值为 5，然后执行自增操作，将其加 1，得到 6。</li>
                    <li>最后，线程 A 和线程 B 分别将自增后的值写回 <code class="bg-purple-200 px-2 py-1 rounded">count</code>，结果 <code class="bg-purple-200 px-2 py-1 rounded">count</code> 的值变成了 6，而不是我们期望的 7。</li>
                </ol>
                <p class="mt-4 text-purple-800 font-semibold">
                    <i class="fas fa-info-circle mr-2"></i>
                    因此，原子操作的意义在于确保对共享资源的访问和修改是线程安全的，避免出现数据不一致或损坏的情况。
                </p>
            </div>
        </div>

        <!-- Atomic Features -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-star mr-3 text-yellow-500"></i>Atomic的核心特性
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="feature-card rounded-lg p-6 text-center">
                    <div class="text-4xl mb-4 text-purple-600">
                        <i class="fas fa-atom"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">原子性操作</h3>
                    <p class="text-gray-600">Atomic 类提供的操作都是原子性的，要么全部执行成功，要么全部不执行，不会出现中间状态。</p>
                </div>
                <div class="feature-card rounded-lg p-6 text-center">
                    <div class="text-4xl mb-4 text-blue-600">
                        <i class="fas fa-eye"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">内存可见性</h3>
                    <p class="text-gray-600">保证了对共享变量的修改对其他线程是可见的，通过内存屏障和 volatile 变量来实现。</p>
                </div>
                <div class="feature-card rounded-lg p-6 text-center">
                    <div class="text-4xl mb-4 text-green-600">
                        <i class="fas fa-exchange-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3">CAS机制</h3>
                    <p class="text-gray-600">基于 CAS（Compare and Swap）机制实现，通过比较当前值与期望值，如果相等则更新为新值。</p>
                </div>
            </div>
        </div>

        <!-- Common Atomic Classes -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-12">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-cubes mr-3 text-indigo-600"></i>Atomic的常用子类
            </h2>
            <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
                <div class="atomic-class-card rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-3 text-purple-700">
                        <i class="fas fa-toggle-on mr-2"></i>AtomicBoolean
                    </h3>
                    <p class="text-gray-600 mb-3">用于原子更新布尔类型，常见的方法包括 <code class="bg-gray-200 px-2 py-1 rounded text-sm">get()</code>、<code class="bg-gray-200 px-2 py-1 rounded text-sm">set()</code>、<code class="bg-gray-200 px-2 py-1 rounded text-sm">getAndSet()</code> 等。</p>
                    <p class="text-sm text-gray-500"><i class="fas fa-check-circle mr-1 text-green-500"></i>适用于开关状态、标志位等场景</p>
                </div>
                
                <div class="atomic-class-card rounded-xl p-6">
                    <h3 class="text-xl font-semibold mb-3 text-purple-700">
                        <i class="fas